local piga = fk.CreateSkill {
    name = "mk__piga",
    tags = { Skill.Switch },
}



piga:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "offensive",
  prompt = function (self, player, selected_cards, selected)
     if player:getSwitchSkillState(piga.name) == fk.SwitchYang then
        return "#mk__piga_1"
     end
     return "#mk__piga_2"
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(piga.name) and data.card.is_damage_card
     and #data.tos == 1
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(piga.name) == fk.SwitchYang then
        local tt = table.filter(room.alive_players, function(r)
        return player~=r
        end)
        local targets = table.filter(tt, function (p)
        return table.every(tt, function (q)
            return  player:distanceTo(p) <= player:distanceTo(q)
            end)
        end)
        if #targets == 0 then return end
        local to = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        skill_name = piga.name,
        cancelable = false,
        })[1]
        if data.tos[1] == to then
            local choice1 = room:askToChoice(player, {
                choices = {"手牌数" , "体力值"},
                skill_name = piga.name
            })
            local choice2 = room:askToChoice(player, {
                choices = {"调整至与其相同" , "调整至与你相同"},
                skill_name = piga.name
            })
            if choice1 == "手牌数" then
                if choice2 == "调整至与其相同" then
                    local n = player:getHandcardNum() - data.tos[1]:getHandcardNum()
                    if n > 0 then
                        room:askToDiscard(player, {
                        min_num = n,
                        max_num = n,
                        include_equip = false,
                        skill_name = piga.name,
                        cancelable = false
                        })
                    else
                        player:drawCards(-n, piga.name)
                    end
                else
                    local n = data.tos[1]:getHandcardNum() - player:getHandcardNum()
                    if n > 0 then
                        room:askToDiscard(data.tos[1], {
                        min_num = n,
                        max_num = n,
                        include_equip = false,
                        skill_name = piga.name,
                        cancelable = false
                        })
                    else
                        data.tos[1]:drawCards(-n, piga.name)
                    end
                end
            elseif choice1 == "体力值" then
                if choice2 == "调整至与其相同" then
                    local n = data.tos[1].hp - player.hp
                    if n > 0 then
                        room:recover{
                            who = player,
                            num = n,
                            recoverBy = player,
                            skillName = piga.name,
                        }
                    else
                        room:loseHp(player, -n, piga.name)
                    end
                else
                    local n = player.hp - data.tos[1].hp
                    if n > 0 then
                        room:recover{
                            who = data.tos[1],
                            num = n,
                            recoverBy = player,
                            skillName = piga.name,
                        }
                    else
                        room:loseHp(data.tos[1], -n, piga.name)
                    end
                end
            end
        end
        if data.toCard then
            data.toCard = nil
        else
            data.tos = {}
        end
        data.tos = {to}
    else
        local tt = table.filter(room.alive_players, function(r)
        return player~=r
        end)
        local targets = table.filter(tt, function (p)
        return table.every(tt, function (q)
            return  player:distanceTo(p) >= player:distanceTo(q)
            end)
        end)
        if #targets == 0 then return end
        local to = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        skill_name = piga.name,
        cancelable = false,
        })[1]
        if data.tos[1] == to then
            local choice1 = room:askToChoice(player, {
                choices = {"手牌数" , "体力值"},
                skill_name = piga.name
            })
            local choice2 = room:askToChoice(player, {
                choices = {"调整至与其相同" , "调整至与你相同"},
                skill_name = piga.name
            })
            if choice1 == "手牌数" then
                if choice2 == "调整至与其相同" then
                    local n = player:getHandcardNum() - data.tos[1]:getHandcardNum()
                    if n > 0 then
                        room:askToDiscard(player, {
                        min_num = n,
                        max_num = n,
                        include_equip = false,
                        skill_name = piga.name,
                        cancelable = false
                        })
                    else
                        player:drawCards(-n, piga.name)
                    end
                else
                    local n = data.tos[1]:getHandcardNum() - player:getHandcardNum()
                    if n > 0 then
                        room:askToDiscard(data.tos[1], {
                        min_num = n,
                        max_num = n,
                        include_equip = false,
                        skill_name = piga.name,
                        cancelable = false
                        })
                    else
                        data.tos[1]:drawCards(-n, piga.name)
                    end
                end
            elseif choice1 == "体力值" then
                if choice2 == "调整至与其相同" then
                    local n = data.tos[1].hp - player.hp
                    if n > 0 then
                        room:recover{
                            who = player,
                            num = n,
                            recoverBy = player,
                            skillName = piga.name,
                        }
                    else
                        room:loseHp(player, -n, piga.name)
                    end
                else
                    local n = player.hp - data.tos[1].hp
                    if n > 0 then
                        room:recover{
                            who = data.tos[1],
                            num = n,
                            recoverBy = player,
                            skillName = piga.name,
                        }
                    else
                        room:loseHp(data.tos[1], -n, piga.name)
                    end
                end
            end
        end
        if data.toCard then
            data.toCard = nil
        else
            data.tos = {}
        end
        data.tos = {to}
    end
  end,
})

Fk:loadTranslationTable{
  ["mk__piga"] = "貔戛",
  [":mk__piga"] = "转换技，当你使用伤害牌指定唯一目标时，可以改为与你距离最①远②近的一名角色为目标。若目标未改变，则你与其的体力或手牌调整至一方。",
  ["#mk__piga_1"] = "可以改为与你距离最 远 的一名角色为目标",
  ["#mk__piga_2"] = "可以改为与你距离最 近 的一名角色为目标",
  ["$mk__piga1"] = "主公有延助力，何愁汉室难兴？",
  ["$mk__piga2"] = "此番斩将得胜，只是连捷之始。",
  ["$mk__piga3"] = "与我为敌，是汝等最大的不幸。",
  ["$mk__piga4"] = "贼寇尚未尽戮，我岂会还营！",
}
return piga